package com.xuan.yrmanage.web.controller;


import com.xuan.yrmanage.common.response.CommonCode;
import com.xuan.yrmanage.common.response.ResponseResult;
import com.xuan.yrmanage.domain.Menu;
import com.xuan.yrmanage.domain.User;
import com.xuan.yrmanage.domain.request.QueryPageBean;
import com.xuan.yrmanage.domain.response.MenuCode;
import com.xuan.yrmanage.service.MenuService;
import com.xuan.yrmanage.web.controller_api.MenuControllerApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * FileName: SysMenuController
 * Author:   xuan zongjun
 * Date:     2020/2/26 10:13
 * Description:
 */

@RestController
@RequestMapping("/menu")
public class MenuController implements MenuControllerApi {
    @Autowired
    private MenuService menuService;

    //分页查询菜单
    @PostMapping("/findAll")
    @Override
    public ResponseResult findAll(@RequestBody QueryPageBean queryPageBean, @ApiIgnore User user) {
        try{

            return menuService.findAll( queryPageBean.getPageIndex(), queryPageBean.getPageSize(), queryPageBean.getQueryString() );
        }catch (Exception ex){
            return  new ResponseResult( CommonCode.FAIL,null);
        }

    }
    //添加菜单
    @Override
    @PostMapping("/add")
    public ResponseResult insertSysMenu(@ApiIgnore Menu menu, String permissionIds, @ApiIgnore User user) {
        try{

            return menuService.insertSysMenu( menu,permissionIds,user.getUserId() );
        }catch (Exception ex){
            ex.printStackTrace();
            return  new ResponseResult( MenuCode.MENU_INSERT_FAIL);
        }

    }
    //修改菜单
    @Override
    @PostMapping("/edit")
    public ResponseResult updateSysMenu(@RequestParam("id") Integer id, Menu menu, String permissionIds, @ApiIgnore User user) {
        try{

            return menuService.updateSysMenu(id,menu,permissionIds,user.getUserId());
        }catch (Exception ex){
            ex.printStackTrace();
            return  new ResponseResult( MenuCode.MENU_EDIT_FAIL);
        }

    }

    //删除菜单
    @Override
    @PostMapping("/deleteById")
    public ResponseResult deleteSysMenu(@RequestParam("menuId")Integer menuId, @ApiIgnore User user) {
        try{


            return menuService.deleteSysMenuById(menuId);
        }catch (Exception ex){
            return  new ResponseResult( MenuCode.MENU_DELETE_FAIL);
        }
    }

    //分等级获取菜单
    @Override
    @GetMapping("/getAllByLevel")
    public ResponseResult getAllByLevel(@ApiIgnore User user) {
        try{

            return menuService.getAllByLevel();
        }catch (Exception ex){
            return  new ResponseResult( MenuCode.MENU_QUERY_FAIL);
        }
    }

    //根据菜单获取下面所有的权限
    @Override
    @PostMapping("/getPermissionByMenuId")
    public ResponseResult getPermissionByMenuId(Integer menuId, @ApiIgnore User user) {
        try{


            return menuService.getPermissionByMenuId(menuId);
        }catch (Exception ex){
            return  new ResponseResult( MenuCode.MENU_DELETE_FAIL);
        }
    }

    //获取一级菜单
    @Override
    @PostMapping("/getOneLevelMenu")
    public ResponseResult getOneLevelMenu(@ApiIgnore User user) {
        try{

            return menuService.getOneLevelMenu();
        }catch (Exception ex){
            return  new ResponseResult( MenuCode.MENU_DELETE_FAIL);
        }
    }
}
